package javaweb.web;

import javaweb.pojo.Book;
import javaweb.pojo.Page;
import javaweb.servlet.impl.BaseServlet;
import javaweb.servlet.impl.BookServletImpl;
import javaweb.servlet.BookServlet;
import javaweb.utility.WebUtils;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author shkstart
 * @creat -08-01 21:19
 */
public class ClientBookServlet extends BaseServlet {
            BookServlet bs= new BookServletImpl();
    public void page(HttpServletRequest request, HttpServletResponse response) {
        //System.out.println("hhhh");
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        Page<Book> page = bs.page(pageNo, pageSize);
        page.setUrl("client/bookServlet?action=page");
        request.setAttribute("page",page);
        try {
            request.getRequestDispatcher("/pages/client/index.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void pageByPrice(HttpServletRequest request, HttpServletResponse response) {
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        int min = WebUtils.parseInt(request.getParameter("min"), 0);
        int max = WebUtils.parseInt(request.getParameter("max"), Integer.MAX_VALUE);
        Page<Book> page = bs.pageByPrice(pageNo, pageSize,min,max);
       StringBuilder sb=new StringBuilder("client/bookServlet?action=pageByPrice");
       if (request.getParameter("min")!=null){
           sb.append("&min").append(request.getParameter("min"));
       }if (request.getParameter("max")!=null){
           sb.append("&max").append(request.getParameter("max"));
       }
        page.setUrl(sb.toString());
        request.setAttribute("page",page);
        try {
            request.getRequestDispatcher("/pages/client/index.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
